package com.youlu.campus.admin.form;

import com.youlu.campus.admin.auth.entity.SystemUser;
import com.youlu.campus.admin.auth.service.UserService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.admin.utils.IpUtils;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.VO.req.UserFormTaskInfoQueryVO;
import com.youlu.campus.entity.form.UserFormTaskInfo;
import com.youlu.campus.service.form.FormTaskConfigService;
import com.youlu.campus.service.form.UserFormTaskInfoService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Objects;

@Slf4j
@RestController
public class UserFormController {
    @Autowired
    private UserFormTaskInfoService userFormTaskInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private FormTaskConfigService formTaskConfigService;

    @PostMapping("/platform/form/user/list")
    public QResult list(@RequestBody @NotNull UserFormTaskInfoQueryVO req) {
        return QResult.success(userFormTaskInfoService.list(req));
    }

    @PostMapping("/platform/form/user/export")
    public QResult export(@RequestBody @NotNull UserFormTaskInfoQueryVO req
            , HttpServletRequest request) {
        req.setUserId(AuthUtils.getUserId());
        SystemUser systemUser = userService.get(req.getUserId());
        if (Objects.isNull(systemUser)) {
            throw new BusinessException("没有权限导出用户表单");
        }
        if (StringUtils.isBlank(req.getFormId())) {
            log.error(":>>> 表单ID不能为空");
            throw new BusinessException("表单Id不能为空");
        }
        String loginName = systemUser.getLoginName();
        log.info("【导出用户表单】loginName:{}", loginName);
        req.setOptUserName(systemUser.getLoginName());
        req.setOptUserRealName(systemUser.getRealName());
        String ip = IpUtils.getIpAddr(request);
        req.setIpAddress(ip);
        return QResult.success(userFormTaskInfoService.export(req));
    }

    @GetMapping("/platform/form/user/delete")
    public QResult delete(@RequestParam("id") String id) {
        return QResult.success(userFormTaskInfoService.delete(id));
    }

    @GetMapping("/platform/form/user/find-one")
    public QResult findOne(@RequestParam("id") String id) {
        UserFormTaskInfo u = userFormTaskInfoService.findOne(id);
        if (Objects.isNull(u)) {
            return QResult.success(null);
        }
        return QResult.success(userFormTaskInfoService.findBy(u.getUserId(), u.getFormTaskConfigId()));
    }


}
